

var wait = 60;
var cce;
$(document).ready(function() {

	$("#bindmail").click(function() {
		$("#popup-verifyemail").dialog({
			title: "绑定邮箱",
			width: 620
		})
	});
	$("#editmail").click(function() {
		$("#popup-verifyeditemail").dialog({
			title: "修改邮箱",
			width: 620
		})
	});
	$("#editpwd").click(function() {
		$("#popup-verifycontact").dialog({
			title: "修改密码",
			width: 620
		})
	});
	$(".get_mail").click(function(){
		$.post('get_code', {
			type: 1,
		}, function(c) {
			if (c.success) {
				dialog.alert("消息",c.message);

				wait = 60;
				clearTimeout(cce);
				time_2($(".get_mail"))
			} else {
				dialog.error("消息",c.message);

			}
		},'json')
	})
	
});

function time_2(a) {
	if (wait == 0) {
		a.removeAttr("disabled");
		a.val("点击获取");
		wait = 60
	} else {
		a.attr("disabled", "disabled");
		a.val("(" + wait + ")秒后重新获取验证码");
		wait--;
		cce = setTimeout(function() {
			time_2(a)
		}, 1000)
	}
}
function checkUserMail() {
	var b = $.trim($("#txtEmail").val());
	var a = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if (b == "") {
		dialog.error("消息", "用户邮箱不能为空！");
		return false
	}
	if (!a.test(b)) {
		dialog.error("消息", "邮箱格式不对！");
		return false
	}
	$("#btnCheckMail").attr("disabled", "disabled");
	$("#btnCheckMail").val("提交中，请等待...");
	$.post('checkMail', {
		type: 1,
		usermail: b
	}, function(c) {
		if (c.success) {
			dialog.alert("消息",c.message);
			$("#mailPage").hide();
			$("#mailCodePage").show();
			$("#btnCheckMail").hide();
			$("#btnCheckMailCode").show();
			$(".step1").removeClass("stepactive");
			$(".step2").addClass("stepactive");
			wait = 60;
			clearTimeout(cce);
			time_2($(".get_mail"))
		} else {
			dialog.error("消息",c.message);
			$("#btnCheckMail").removeAttr("disabled");
			$("#btnCheckMail").val("下一步")
		}
	},'json')
}
function bindMail() {
	var a = $.trim($("#txtEmailCode").val());
	
	if (a == "") {
		dialog.error("消息", "验证码不能为空！");
		return false
	}
	$("#btnCheckMailCode").attr("disabled", "disabled");
	$("#btnCheckMailCode").val("提交中，请等待...");
	$.post('bindMail', {
		type: 1,
		code: a,
		usermail: $.trim($("#txtEmail").val())
		
	}, function(b) {
		if (b.success) {
			$(".step2").removeClass("stepactive");
			$(".step3").addClass("stepactive");
				dialog.alert("消息",b.message,null,'/index.php/UserCenter/index');


		} else {
			dialog.error("消息",b.message);
			$("#btnCheckMailCode").removeAttr("disabled");
			$("#btnCheckMailCode").val("下一步")
		}
	},'json')
}

function checkEditMail() {
	var b = $.trim($("#txtEmail2").val());
	var a = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if (b == "") {
		dialog.error("消息", "用户邮箱不能为空！");
		return false
	}
	if (!a.test(b)) {
		dialog.error("消息", "邮箱格式不对！");
		return false
	}
	$("#btnCheckMail2").attr("disabled", "disabled");
	$("#btnCheckMail2").val("提交中，请等待...");
	$.post('checkEditMail', {
		type: 1,
		usermail: b
	}, function(c) {
		if (c.success) {
			dialog.alert("消息",c.message);
			$("#mailPage2").hide();
			$("#mailCodePage2").show();
			$("#btnCheckMail2").hide();
			$("#btnCheckMailCode2").show();
			$(".step1").removeClass("stepactive");
			$(".step2").addClass("stepactive");
			wait = 60;
			clearTimeout(cce);
			time_2($(".get_mail"))
		} else {
			dialog.error("消息",c.message);
			$("#btnCheckMail2").removeAttr("disabled");
			$("#btnCheckMail2").val("下一步")
		}
	},'json')
}
function editEmail() {
	var a = $.trim($("#txtEmailCode2").val());
	if (a == "") {
		dialog.error("消息", "验证码不能为空！");
		return false
	}
	$("#btnCheckMail2").attr("disabled", "disabled");
	$("#btnCheckMail2").val("提交中，请等待...");
	$.post('editMail', {
		type: 1,		
		usermail: $.trim($("#txtEmail2").val()),
		code: a
	}, function(d) {
		if (d.success) {
				$(".step2").removeClass("stepactive");
				$(".step3").addClass("stepactive");
				dialog.alert("消息",d.message,null,'/index.php/UserCenter/index');

	
			$("#popup-verifyeditemail").dialog("close");
			$("#popup-verifyemail").dialog({
				title: "绑定邮箱",
				width: 620
			})
		} else {
			dialog.error("消息",d.message);
			$("#btnEditMail").removeAttr("disabled");
			$("#btnEditMail").val("下一步")
		}
	},'json')
};